import tornado.websocket
import tornado.web
from db import db
from util.myredis import r
conn = {}
class SockHandler(tornado.websocket.WebSocketHandler):
    # 跨域支持
    def check_origin(self, origin):
        return True
    #建立连接
    def open(self):
        #获取参数
        id = int(self.get_query_argument("id"))
        #存储链接
        conn[id] = self
        # r.hset_add('connid','id',conn[id])
    #接收消息
    def on_message(self,mes):
        print(mes)

    def on_close(self):
        pass
import json,time
class ZZ1handler(tornado.web.RequestHandler):
    def get(self):
        while True:
            time.sleep(5)
            id = int(self.get_query_argument("id"))
            userid = int(self.get_query_argument("userid"))
            print(userid)
            code = r.list_lpop('dengdaizhanshi')
            if code:
                code = int(code.decode())
                sql = "select * from landmarks where audit_userid=%d and code=%d union all select * from landmarks1 where audit_userid=%d and code=%d union all select * from landmarks2 where audit_userid=%d and code=%d"% (userid, code,userid,code, userid,code)
                res = db.findAll(sql)
                for i in res:
                    i['audit_time'] = str(i['audit_time'])
                    i['exptime'] = str(i['exptime'])
                    i['interest_rate'] = float(i['interest_rate'])
                    i['code'] = str(i['code'])
                print(res)
                conn[id].write_message(json.dumps(res))
            else:
                print('取完了')


